package com.hospital.pharmacy.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hospital.pharmacy.entity.Purchase;
import com.hospital.pharmacy.service.MedicineService;
import com.hospital.pharmacy.service.PurchaseService;
import com.sun.net.httpserver.HttpContext;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;



/*
 * @program: Hospital
 * @description: 进销
 * @author: 杨曼珍
 * @create: 2018-12-17 19:13
 */

@Controller
@RequestMapping(value = "/purchase")
public class PurchaseController {

    @Autowired
    PurchaseService purchaseService;

    @RequestMapping(value = "/add",produces = "application/json;charset=utf-8",method = RequestMethod.POST)
   @ResponseBody
    public String insertMed(@RequestBody String data)throws ParseException {
        JSONObject jsonObject= JSON.parseObject(data);
        String mdc_id=jsonObject.getString("mdcNum");
        String price=jsonObject.getString("pchPrice");
        String count=jsonObject.getString("pchCount");
        String total=jsonObject.getString("pchTotal");
        String time=jsonObject.getString("pchDate");
        Date date=new SimpleDateFormat("yyyy-MM-dd").parse(time);
        String guarantee_date=jsonObject.getString("pchGuaranteeDate");
        Date guarantee=DateUtils.addMonths(date,Integer.parseInt(guarantee_date));
        return purchaseService.insertPurchase(mdc_id,Double.parseDouble(price), Integer.parseInt(count), Double.parseDouble(total),  new SimpleDateFormat("yyyy-MM-dd").parse(time), guarantee, "新的进货");

    }
    @RequestMapping(value = "/all",produces = "application/json;charset=utf-8",method = RequestMethod.GET)
    @ResponseBody
    public String getPurchase(){
        return purchaseService.selectAllPurchase();
    }

    @RequestMapping(value = "/med_name",produces = "application/json;charset=utf-8",method = RequestMethod.POST)
    @ResponseBody
    public String getByMedName(@RequestBody String data){
        System.out.println(data);
        if (data!=null) {
            String string = JSON.parseObject(data).getString("mdcName");
            System.out.println(string);
            return purchaseService.selectMedByName(string);
        }else
            return JSON.toJSONString("该药品无进货");
    }

    @RequestMapping(value = "/sup_name",produces = "application/json;charset=utf-8",method = RequestMethod.POST)
    @ResponseBody
    public String getBySupName(@RequestBody String data){
        System.out.println(data);
        String string = JSON.parseObject(data).getString("supName");
        System.out.println(string);
        return purchaseService.selectPurchaseBySupName(string);
    }
    @RequestMapping(value = "/delete",produces = "application/json;charset=utf-8",method = RequestMethod.POST)
    @ResponseBody
    public String deleteById(@RequestBody String  data){
        System.out.println(data);
        String string = JSON.parseObject(data).getString("pchId");
        System.out.println(string);
        return purchaseService.deletePurchaseById(Integer.parseInt(string));


    }
    @RequestMapping(value = "/update",produces = "application/json;charset=utf-8",method = RequestMethod.POST)
    @ResponseBody
    public String updatePurchase(@RequestBody String data)throws ParseException{
        Integer pchId = JSON.parseObject(data).getInteger("pchId");
        Double pchPrice = JSON.parseObject(data).getDouble("pchPrice");
        Double pchTotal = JSON.parseObject(data).getDouble("pchTotal");
        Integer pchCount = JSON.parseObject(data).getInteger("pchCount");
        String string = JSON.parseObject(data).getString("pchDate");
        Date pchDate=new SimpleDateFormat("yyyy-MM-dd").parse(string);
        String string1 = JSON.parseObject(data).getString("pchGuaranteeDate");
        Date pchGuaranteeDate=new SimpleDateFormat("yyyy-MM-dd").parse(string1);
        return purchaseService.updatePurchaseByPurchase(pchId,pchPrice,pchTotal,pchCount,pchDate,pchGuaranteeDate);
    }


    @RequestMapping(value = "/date",produces = "application/json;charset=utf-8",method = RequestMethod.POST)
    @ResponseBody
    public String selectPurchaseByDate(@RequestBody String data)throws ParseException {
        System.out.println(data);
        String string = JSON.parseObject(data).getString("pchDate");
        System.out.println(string);

        return purchaseService.selectPurchaseByDate(new SimpleDateFormat("yyyy-MM-dd").parse(string));
    }


}
